package boerse.main.Gui;

/**
 * Created with IntelliJ IDEA.
 * User: 13102012
 * Date: 26.04.13
 * Time: 09:07
 * To change this template use File | Settings | File Templates.
 */

import boerse.main.Asset.Share;

import javax.swing.*;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ViewerDemo extends JFrame {
    private static final int TICK_PERIOD = 1000;
    private Timer ticker;
    private JLabel clockLabel;
    private Share[] allShares;

    private class TickerTask extends TimerTask {
        public void run() {
            String output = createText();
            clockLabel.setText(output);
            clockLabel.repaint();
        }

        private String createText() {
            String output = "<html><body>Current Share Values: <br> <br>";
            output = shareText(output);
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            DateFormat dateFormatter = DateFormat.getDateTimeInstance();
            output += "<br>" + dateFormatter.format(date) + "</body></html>";
            return output;
        }

        private String shareText(String output) {
            for (int i = 0; i < allShares.length; i++) {
                output = output + allShares[i].getName() + " : " + allShares[i].getPrice() + "<br>";
            }
            return output;
        }
    }


    public ViewerDemo(Share[] allShares) {
        clockLabel = new JLabel("Share Values");
        add("Center", clockLabel);
       // setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 300);
        setVisible(true);
        this.allShares=allShares;
    }

    public void start(Timer timer) {
        ticker = timer; //as daemon
        ticker.scheduleAtFixedRate(new TickerTask(), 1000, TICK_PERIOD);
    }
}
